package com.otitan.forest.right.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.forest.right.entity.SysRoleEntity;
import com.otitan.forest.right.mapper.SysRoleMapper;
import com.otitan.forest.right.service.SysRoleService;
import com.otitan.webapp.framework.basepro.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {
@Autowired
private SysRoleMapper sysRoleMapper;

    @Override
    public void checkInsertRole(SysRoleEntity sysRoleEntity) {
        if (sysRoleEntity == null) {
            throw new BusinessException("角色信息不能为空！");
        }
        LambdaQueryWrapper<SysRoleEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.eq(SysRoleEntity::getName, sysRoleEntity.getName());
        SysRoleEntity sysRole = getOne(queryWrapper);
        if(sysRole!=null){
            throw new BusinessException(sysRoleEntity.getName()+"已存在");
        }
    }

    @Override
    public void checkUpdateRole(SysRoleEntity sysRoleEntity) {
        LambdaQueryWrapper<SysRoleEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.eq(SysRoleEntity::getName, sysRoleEntity.getName());
        SysRoleEntity sysRole = getOne(queryWrapper);
        if(sysRole!=null && !sysRole.getId().equals(sysRoleEntity.getId())){
            throw new BusinessException(sysRoleEntity.getName()+"已存在");
        }
    }

    @Override
    public List<SysRoleEntity> findRoleByUserId(String userid) {
        return sysRoleMapper.findRoleByUserId(userid);
    }
}